package com.sun.talk.controller

import com.sun.talk.entity.User
import com.sun.talk.entity.bo.UserBO
import com.sun.talk.entity.vo.UserVO
import com.sun.talk.enums.OperatorFriendRequestTypeEnum
import com.sun.talk.enums.SearchFriendsStatusEnum
import com.sun.talk.service.UserService
import com.sun.talk.utils.DigestUtils
import com.sun.talk.utils.Result
import org.springframework.beans.BeanUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("user")
class UserController {

    @Autowired
    lateinit var userService: UserService

    @PostMapping("/registerOrLogin")
    fun registerOrLogin(@RequestBody user: User): Result {
        //用户名密码不能为空
        if(user.username.isEmpty() || user.password.isEmpty()){
            return Result.errorJson(500,"用户名或密码不能为空")
        }
        var result = userService.queryUserByUsername(user.username)
        //如果存在登录 不存在注册
        if(result != null){
            result = userService.queryUserForLogin(user.username,DigestUtils.getMd5(user.password))
            if(result == null){
                return Result.errorJson(500,"用户名或密码不正确")
            }
        }else{
            user.apply {
                nickname = username
                faceImage = ""
                faceImageBig = ""
                password = DigestUtils.getMd5(password)
            }
            result = userService.saveUser(user)
        }
        if(result == null){
            return Result.errorJson(500,"未知错误")
        }
        val userVO = UserVO()
        BeanUtils.copyProperties(result,userVO)
        return Result.successJson(userVO)
    }

    /**
     * 设置用户昵称
     */
    @PostMapping("/setNickname")
    fun setNickname(@RequestBody userBO: UserBO): Result {
        val user = User()
        user.id = userBO.id
        user.nickname = userBO.nickname

        val result = userService.updateUserInfo(user)
        return Result.successJson(result)
    }

    /**
     * 搜索好友
     */
    @PostMapping("/search")
    fun searchUser(myUserId:String,friendUserName:String): Result {
        //参数不能为空
        if(myUserId.isEmpty() || friendUserName.isEmpty()){
            return Result.errorJson(500,"参数错误")
        }
        // 搜索的用户不存在
        // 搜索的用户是你自己
        // 搜索的用户已经你的好友
        val status = userService.preconditionSearchFriends(myUserId, friendUserName)
        if(status != SearchFriendsStatusEnum.SUCCESS.status){
            return Result.errorJson(500, SearchFriendsStatusEnum.getMsgByKey(status))
        }
        val user = userService.queryUserByUsername(friendUserName)!!
        val userVO = UserVO()
        BeanUtils.copyProperties(user,userVO)
        return Result.successJson(userVO)
    }

    /**
     * 发送添加好友请求
     */
    @PostMapping("/addFriendRequest")
    fun addFriendRequest(myUserId:String,friendUserName:String): Result {
        //参数不能为空
        if(myUserId.isEmpty() || friendUserName.isEmpty()){
            return Result.errorJson(500,"参数错误")
        }
        // 搜索的用户不存在
        // 搜索的用户是你自己
        // 搜索的用户已经你的好友
        val status = userService.preconditionSearchFriends(myUserId, friendUserName)
        if(status != SearchFriendsStatusEnum.SUCCESS.status){
            return Result.errorJson(500, SearchFriendsStatusEnum.getMsgByKey(status))
        }
        userService.sendFriendRequest(myUserId,friendUserName)
        return Result.successJson()
    }

    /**
     * 好友请求列表
     */
    @GetMapping("/queryFriendRequests")
    fun queryFriendRequests(userId:String): Result {
        //参数不能为空
        if(userId.isEmpty()){
            return Result.errorJson(500,"参数错误")
        }
        return Result.successJson(userService.queryFriendRequestList(userId))
    }

    /**
     * 通过或忽略好友请求
     */
    @PostMapping("/operFriendRequest")
    fun operFriendRequest(acceptUserId:String,sendUserId:String,operType:Int?): Result {
        //参数不能为空
        if(acceptUserId.isEmpty() || sendUserId.isEmpty() || operType == null){
            return Result.errorJson(500,"参数错误")
        }
        if(OperatorFriendRequestTypeEnum.getMsgByKey(operType).isEmpty()){
            return Result.errorJson(500,"参数错误")
        }

        if(operType == OperatorFriendRequestTypeEnum.IGNORE.type){
            //忽略好友请求 删除好友请求
            userService.deleteFriendRequest(sendUserId, acceptUserId)
        }else if (operType == OperatorFriendRequestTypeEnum.PASS.type){
            //同意好友请求 互相增加好友记录 删除好友请求
            userService.passFriendRequest(sendUserId, acceptUserId)
        }
        return Result.successJson(userService.queryFriendList(acceptUserId))
    }

    /**
     * 通过或忽略好友请求
     */
    @GetMapping("/myFriends")
    fun myFriends(userId:String): Result {
        //参数不能为空
        if(userId.isEmpty()){
            return Result.errorJson(500,"参数错误")
        }
        //查询好友列表
        return Result.successJson(userService.queryFriendList(userId))
    }

    /**
     * 获取未签收消息列表
     */
    @GetMapping("/getUnReadMsgList")
    fun getUnReadMsgList (acceptUserId:String): Result {
        //参数不能为空
        if(acceptUserId.isEmpty()){
            return Result.errorJson(500,"参数错误")
        }
        //查询好友列表
        return Result.successJson(userService.queryUnReadMsgList(acceptUserId))
    }
}